package com.neutech.code.view;
import com.neutech.code.dao.AdminDAO;
import com.neutech.code.dao.impl.AdminDAOImpl;
import com.neutech.code.entity.Admin;

import static  com.neutech.code.constant.Constant.input;

public class AdminView {
    private static AdminDAO adminDAO=new AdminDAOImpl();
    public static void showMenu(){
        System.out.println("欢迎使用饿了么管理系统");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出");
        System.out.println("请输入编号选择功能");
        int index=input.nextInt();
        if(index==1){
            showLogin();
        }else if(index==2){
            register();
        }else if(index==3){

        }else{
            System.err.println("没有此功能");
        }

    }

    public static void register(){
        System.out.println("请输入用户名");
        String adminName = input.next();
        System.out.println("清输入密码");
        String password = input.next();

        Admin admin = adminDAO.selectByAdminName(adminName);
        if(admin.getAdminName()!=null){
            System.out.println("用户已存在");
        }else{
            Admin adminData=new Admin();
            adminData.setAdminName(adminName);
            adminData.setPassword(password);
            adminDAO.insert(adminData);
            System.out.println("注册成功");
        }
        showMenu();
    }

    public static void showLogin() {
        System.out.println("请输入用户名");
        String adminName = input.next();
        System.out.println("清输入密码");
        String password = input.next();
        Admin admin = adminDAO.selectByAdminName(adminName);


        if (admin.getAdminId() == null) {
            System.err.println("没有此用户");
            showMenu();
        } else if (!admin.getPassword().equals(password)) {
            System.out.println("用户名或密码错误");
            showMenu();
        } else {
            System.out.println("登陆成功");
            BusinessView.showMenu();
        }



    }
}
